//
//  SHWithdrawCashBankCardController.m
//  JiFengDaRen
//
//  Created by SH_iOS on 2017/7/20.
//  Copyright © 2017年 SH_iOS. All rights reserved.
//
#import "SHBankCarCell.h"
#import "BankCardModel.h"
#import "SHWithdrawCashBankCardController.h"

@interface SHWithdrawCashBankCardController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *bankCardDataArray;


@end

@implementation SHWithdrawCashBankCardController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"选择银行卡";
    [self configUI];
    [self requstBankCardInfo:YES];

    // Do any additional setup after loading the view.
}
- (NSMutableArray *)bankCardDataArray{
    if (!_bankCardDataArray) {
        _bankCardDataArray = [NSMutableArray arrayWithCapacity:0];
    }
    
    return _bankCardDataArray;
}


- (UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight - KNAVIGATIONBARHEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
        _tableView.separatorStyle =  UITableViewCellSeparatorStyleNone;
        [_tableView registerClass: [SHBankCarCell  class]  forCellReuseIdentifier:@"BankCard"];
        
    }
    return _tableView;
}

#pragma mark -- 初始化UI
- (void)configUI{
    
    self.view.backgroundColor = KBASEBGCOLOR;
   
}

#pragma mark -- 获取银行卡信息
- (void)requstBankCardInfo:(BOOL)isRemove{
    
    [SHUserCenterHttp getBankCardLsitWithUsingChoices:1 bankCardType:11 success:^(NSMutableArray *bankCardListArray) {
        if (isRemove) {
            [self.bankCardDataArray removeAllObjects];
            
        }
        for (BankCardModel *model in bankCardListArray) {
            model.using_choices = 1;
            if (model.is_auth == 1 || model.auth_4_status) {
                [self.bankCardDataArray addObject:model];
            }
        }
        [self.tableView.mj_header endRefreshing];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.tableView reloadData];
            
        });
        
        
        
    } complection:^(id result) {
        [self.tableView.mj_header endRefreshing];
        
    } failure:^(id erro) {
        [self.tableView.mj_header endRefreshing];
        
    }];
    
    
    
}

#pragma mark --- UITablView  & DataSource  & Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.bankCardDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SHBankCarCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BankCard"];
    
    BankCardModel *model = self.bankCardDataArray[indexPath.row];
    cell.model = model;
    cell.default_useLabel.hidden = YES;

    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return __I6Height(60);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    BankCardModel *model = self.bankCardDataArray[indexPath.row];
    if (_delegate && [_delegate respondsToSelector:@selector(withdrawCashBankCard:)]) {
        [_delegate withdrawCashBankCard:model];
        dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25/*延迟执行时间*/ * NSEC_PER_SEC));
        
        dispatch_after(delayTime, dispatch_get_main_queue(), ^{
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
            [self.navigationController popViewControllerAnimated:YES];
            
        });
    
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
